package dao;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import entity.Account;

public class JdbcAccountDAO implements IAccountDAO {

	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void save(Account account) {
		String sql = "insert into t_account(balance) values(?)";
		System.out.println("sql=" + sql);
		jdbcTemplate.update(sql, new Object[] { account.getBalance() });
	}

	public void update(Account account) {
		String sql = "update t_account set balance=? where id=?";
		System.out.println("sql=" + sql);
		jdbcTemplate.update(sql, new Object[] { account.getBalance(),
				account.getId() });
	}

	public void delete(Account account) {
		String sql="delete from t_account where id=?";
		jdbcTemplate.update(sql, new Object[]{account.getId()});
	}

	public Account getById(int id) {
		String sql = "select * from t_account where id=?";
		Account account = (Account) jdbcTemplate.queryForObject(sql,
				new Object[] { id }, new AccountRowMapper());
		return account;
	}

	public List<Account> getAll() {
		String sql = "select * from t_account";
		List<Account> accounts = (List<Account>) jdbcTemplate.query(sql,
				new AccountRowMapper());

		return accounts;
	}

}
